#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.db import models

# Create your models here.
import datetime
from django.contrib.auth.models import User
from django.utils.encoding import python_2_unicode_compatible
from django.utils import timezone
from django.contrib.auth.models import AbstractUser

#用户表
#@python_2_unicode_compatible    # django内置的兼容python3的Unicode语法的一个装饰器
class NewUser(AbstractUser):
    profile = models.CharField('profile',default='',max_length=256) # 简介

    def __str__(self):  # __str__方法是为了后台管理admin和django shell的显示，Meta类也是为了后台显示服务的。
        return self.username

# 文章类型表

class Column(models.Model):
    name = models.CharField('column_name', max_length=256)
    intro = models.TextField('introduction', default='')  # 简介

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'column'
        verbose_name_plural = 'column'
        ordering = ['name']

#文章表

class Article(models.Model):
    column = models.ForeignKey(Column,blank=True,null=True,verbose_name='belong to')    # 文章类型
    title=models.CharField(max_length=256)  # 标题
    content=models.TextField('content') # 文章内容
    author = models.ForeignKey('Author')    # 作者
    user = models.ManyToManyField('NewUser',blank=True) # 普通用户
    create_date =models.DateTimeField(auto_now_add=True,editable=True)
    update_date =models.DateTimeField(auto_now=True,null=True)
    published = models.BooleanField('notDraft',default=True)    # 是否发布
    comment_num = models.IntegerField(default=0)    # 评论数
    poll_num = models.IntegerField(default=0)   #点赞数
    keep_num = models.IntegerField(default=0)   # 收藏数
    def __str__(self):
        return self.title
    class Meta:
        verbose_name = 'article'
        verbose_name_plural = 'article'


# 评论表

class Comment(models.Model):
    artilce = models.ForeignKey(Article,null=True)
    user = models.ForeignKey('NewUser',null=True)
    content = models.TextField()    # 评论内容
    create_date = models.DateTimeField(auto_now_add=True,editable=True)
    poll_num = models.IntegerField(default=0)   # 点赞数

    def __unicode__(self):
        return self.content

#作者表

class Author(models.Model):
    name =models.CharField(max_length=256)
    profile =models.CharField('profile',default='',max_length=256)  #简介
    password =models.CharField('password',max_length=256)
    register_date = models.DateTimeField(auto_now_add=True,editable=True)
    def __unicode__(self):
        return self.name
#点赞表

class Poll(models.Model):
    artile = models.ForeignKey(Article,null=True)
    user = models.ForeignKey('NewUser',null=True)
    comment = models.ForeignKey(Comment,null=True)
